﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Pub_Module_News : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ViewAll"] != "Y")
            {
                //td1.Attributes.Remove("class");
                //td2.Attributes.Remove("class");
                //td3.Attributes.Remove("class");
            }
            BindData();           

        }
        
    }

   

    public string LimitWord(string value)
    {
        //String html = "Whatever your html is";
        //html = Regex.Replace(html, @"</?[^>]*>", ""); 
        value = StripHtml(value, false);

        int length = 400;
        if (value.Length > length)
            return "&nbsp;&nbsp;"+value.Substring(0, length) + "...";
        else
            return "&nbsp;&nbsp;" + value;

    }

    public static string StripHtml(string html, bool allowHarmlessTags)
    {
        if (html == null || html == string.Empty)
            return string.Empty;

        if (allowHarmlessTags)
            return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);

        return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
    }

    private void BindData()
    {
        
        string viewcount=System.Configuration.ConfigurationManager.AppSettings["TopNews"];
        grvNews.AllowPaging = false;
        if (Request.QueryString["ViewAll"] == "Y")
        {
            //hplViewAll.Visible = false;
            grvNews.AllowPaging = true;
            viewcount=System.Configuration.ConfigurationManager.AppSettings["AllNews"];
            grvNews.PageSize = int.Parse(viewcount);
            viewcount = "0";
      
        }

      
        string KindNo = "N";
        if (Request.QueryString["KindNo"] != "" && Request.QueryString["KindNo"] != null)
            KindNo = Request.QueryString["KindNo"];


        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("NewsID", "0");
        msg.CreateFirstNode("Title", "");
        msg.CreateFirstNode("KindNo", cls.killSQLInjection(KindNo));
        msg.CreateFirstNode("Status", "");
        msg.CreateFirstNode("ViewCount", viewcount);

        DataSet ds = null;
        ds = cls.Get_NewsByCondition(msg.GetXmlStr);
        
        grvNews.DataSource = ds;
        grvNews.DataBind();
        
     

    }
    
    protected void grvNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType==DataControlRowType.DataRow)
        {
            Image imgNews = (Image)e.Row.FindControl("imgNews");
            string ImgUrl = DataBinder.Eval(e.Row.DataItem, "ImgUrl").ToString();
            if(ImgUrl=="" || ImgUrl==null)
                   ImgUrl= "~/Images/Body/Noimages.gif";
            else
                ImgUrl="~/"+ ImgUrl;
            imgNews.ImageUrl = ImgUrl;
           /* HyperLink hplTitle=(HyperLink)e.Row.FindControl("hplTitle");
            hplTitle.NavigateUrl="~/Common/News/NewsDetail.aspx?NewsID=" + DataBinder.Eval(e.Row.DataItem,"NewsID")+ "&KindNo="+ DataBinder.Eval(e.Row.DataItem,"KindNo") ;*/
            Label lblDate = (Label)e.Row.FindControl("lblDate");
            lblDate.Text = "Cập nhật ngày: "+ FormatDate(lblDate.Text);

        }
    }
    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    protected void grvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNews.PageIndex = e.NewPageIndex;
        BindData();
    }
}
